package graphics;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;
import javax.swing.Timer;

import physics.AchtungBall;
import physics.Ball;
import physics.ImmovableObstacle;
import physics.Vector;
import physics.World;

public class Pong extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private World w;
	private final int FPS = 30;
	
	
	/**
	 * Create the panel.
	 */
	public Pong() {
		w = new World();
		this.setMinimumSize(new Dimension(800,800));
		this.setMaximumSize(new Dimension(1900,1080));
		this.setPreferredSize(ComponentFrame.getScreenSize());
		this.setSize(getPreferredSize());
		
		WorldDrawer.DRAW_VECTORS = false;
		
		//Circle field = new Circle(new Coordinate(200,100), 100, 100);
		AchtungBall ball = new AchtungBall(200,200,5,10,new Vector(Math.toRadians(-15),100));
		//Ball ball = new Ball(200,200,20,10,new Vector(Math.toRadians(-15),50));
		//w.addObstacle(field);
		//Shape shape = new Circle();
		//ImmovableObstacle obstacle = new ImmovableObstacle(location, bound, mass)
		w.addObstacle(ball);
		this.addKeyListener(ball);
		setFocusable(true);
		Timer t = new Timer(1000/FPS, new ActionListener(){



			//timer to set when to draw the screen and update values
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Pong.this.repaint();
			}

		});
		t.start();
		this.repaint();
		w.start();
	}
	
	BufferedImage img;
	
	public void paint(Graphics g){
		//img =(BufferedImage) createImage(this.getWidth(),this.getHeight());
		img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2D = (Graphics2D) img.createGraphics();
		WorldDrawer.drawWorld(g2D, w);
		g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
	}
	
	public void update(Graphics g){
		paint(g);
	}
	
	public static void main(String[] args){
		ComponentFrame.showFrame("Pong", new Pong());
	}
	

}
